<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>医生排班管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-form inline>
        <el-form-item label="开始日期" label-width="70px">
          <el-date-picker v-model="queryInfo.startTime" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束日期" label-width="70px">
          <el-date-picker v-model="queryInfo.endTime" type="date" placeholder="选择日期"></el-date-picker>
        </el-form-item>
        <el-button type="primary" @click="getDataList">查询</el-button>
        <el-button type="primary" @click="jump('/schedulingmananger/addrule')">新增排班规则</el-button>
        <el-button type="primary" @click="addScheduing">生成排班计划</el-button>
      </el-form>
      <el-table :data="datalist" border stripe>
        <el-table-column label="日期" prop="scheddate" :formatter="dateFormat"></el-table-column>
        <el-table-column label="科室" prop="department.deptname"></el-table-column>
        <el-table-column label="医生" prop="user.realname"></el-table-column>
        <el-table-column label="午别" prop="noon"></el-table-column>
        <el-table-column label="挂号级别" prop="registLevel.registname"></el-table-column>
        <el-table-column label="挂号限额" prop="registLevel.registquota"></el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pageNum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      queryInfo: {
        startTime: "",
        endTime: "",
        pageNum: 1,
        pageSize: 5
      },
      datalist: [],
      departmentList: [],
      total: 0
    };
  },
  created() {
    this.getDataList();
  },
  methods: {
    async getDataList() {
      const { data: res } = await this.$http.post(
        "scheduling/findSchedulingByPage",
        this.queryInfo
      );
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.datalist = res.Schedulings;
      this.total = res.totalpage;
      console.log(res);
    },
    //生成排班计划
    async addScheduing(){
      const confirmResult = await this.$confirm(
        "生成新的计划将会覆盖旧的计划，是否生成？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(err => err);
      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消生成");
      }
      const { data: res } = await this.$http.get(
      "scheduling/addScheduling"
      )
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      }
      this.$message.success(res.meta.msg);
      let date=new Date();
      let formatDate
      date.setDate(1)
      this.queryInfo.startTime = moment(date).format("YYYY-MM-DD");
      date.setMonth(date.getMonth()+1)
      date.setDate(0)
      this.queryInfo.endTime = moment(date).format("YYYY-MM-DD");
      this.getDataList();
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getDataList();
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      this.queryInfo.pageNum = newPage;
      this.getDataList();
    },
    //链接的激活状态
    jump(path) {
      this.$router.push({path: path})
    },
    //格式化日期
    dateFormat(row, column) {
      let date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD");
    }
  }
};
</script>

<style>
</style>